<?php

namespace app\Admin\controller;

//引入公共控制器
use app\common\controller\Backend;

use think\Request;

class Recyclebin extends Backend
{
    public function __construct()
    {
        //继承父类
        parent::__construct();
        $this->PostModel = Model('Post.Post');
        $this->CateModel = Model('Post.Cate');
        $this->CommentModel = Model('Post.Comment');
        $this->ConfigModel = model('Config');
        $this->ProductModel = model('Product.Product');

        $this->UserModel = Model('User.User');
        $this->RecordModel = Model('User.Record');
    }
    // 配置
    public function config()
    {
        // 配置总数
        $count = $this->ConfigModel->onlyTrashed()->count();
        // 关键字
        $keywords = $this->request->param('keywords', '');

        $where = [];

        // 关键字
        if ($keywords) {
            $where['title'] = ['like', "%$keywords%"];
        }
        $countlist = $this->ConfigModel->onlyTrashed()->where($where)->paginate(10);

        $pagestr = $countlist->render();

        $this->assign([
            'keywords' => $keywords,
            'count' => $count,
            'countlist' => $countlist,
            'pagestr' => $pagestr
        ]);

        return $this->fetch();
    }
    // 用户回收站
    public function recyclebin()
    {
        $start = $this->request->param('start', '');

        $end = $this->request->param('end', '');

        $keywords = $this->request->param('keywords', '');

        $where = [];

        // 开始时间
        if ($start) {
            $where['createtime'] = ['> time', $start];
        }

        // 结束时间  小于某个时间
        if ($end) {
            $where['createtime'] = ['<= time', $end];
        }
        // 时间区间
        if ($start && $end) {
            $where['createtime'] = ['between time', [$start, $end]];
        }
        // 关键字
        if ($keywords) {
            if (preg_match('/@/i', $keywords)) {
                $where['email'] = ['like', "%$keywords%"];
            } else {
                $where['nickname'] = ['like', "%$keywords%"];
            }
        }
        $count = $this->UserModel->where($where)->count();
        // query： 分页调用的时候传参，request请求传过来的参数
        $userlist = $this->UserModel->onlyTrashed()->with('districts')->where($where)->order('id', 'desc')->paginate(10, false, ['query' => Request::instance()->param()]);
        $pagestr = $userlist->render();
        $this->assign([
            'count' => $count,
            'userlist' => $userlist,
            'pagestr' => $pagestr,
            'start' => $start,
            'end' => $end,
            'keywords' => $keywords
        ]);
        return $this->fetch();
    }
    // 消费充值
    public function recordarge()
    {
        $start = $this->request->param('start', '');

        $end = $this->request->param('end', '');

        $keywords = $this->request->param('keywords', '');
        // 是否消费/充值
        $recharge = $this->request->param('recharge', false);
        $where = [];
        $record =  $this->RecordModel;
        // 开始时间
        if ($start) {
            $where['record.createtime'] = ['> time', $start];
        }

        // 结束时间  小于某个时间
        if ($end) {
            $where['record.createtime'] = ['<= time', $end];
        }
        // 时间区间
        if ($start && $end) {
            $where['record.createtime'] = ['between time', [$start, $end]];
        }

        // 关键字
        if ($keywords) {
            $where['user.nickname'] = ['like', "%$keywords%"];
        }
        if ($recharge) {
            $where['state'] = ['=', '3'];
        }
        $count = $this->RecordModel->onlyTrashed()->with('user')->where($where)->count();
        // query： 分页调用的时候传参，request请求传过来的参数
        $record = $this->RecordModel->onlyTrashed()->with('user')->where($where)->order('record.createtime', 'desc')->paginate(10, false, ['query' => Request::instance()->param()]);
        foreach ($record as $key => $item) {
            switch ($item['state']) {
                case 1:
                    $item['state'] = '发布';
                    $item['point'] = '-' . $item['point'];
                    break;
                case 2:
                    $item['state'] = '采纳';
                    $item['point'] = '+' . $item['point'];
                    break;
                case 3:
                    $item['state'] = '充值';
                    $item['point'] = '+' . $item['point'];

                    break;
                case 4:
                    $item['state'] = '签到';
                    $item['point'] = '+' . $item['point'];

                    break;
                case 5:
                    $item['state'] = '下商品订单';
                    $item['point'] = '-' . $item['point'];

                    break;
                case 6:
                    $item['state'] = '下预约订单';
                    $item['point'] = '-' . $item['point'];

                    break;
                case -1:
                    $item['state'] = '退款';
                    $item['point'] = '+' . $item['point'];

                    break;
                default:
                    break;
            }
        }
        $pagestr = $record->render();

        $this->assign([
            'count' => $count,
            'record' => $record,
            'pagestr' => $pagestr,
            'start' => $start,
            'end' => $end,
            'recharge' => $recharge,
            'keywords' => $keywords
        ]);
        return $this->fetch();
    }
    // 悬赏分类
    public function postrecycling()
    {
        $start = $this->request->param('start', '');

        $end = $this->request->param('end', '');

        $keywords = $this->request->param('keywords', '');
        // 是否为悬赏
        $offerareward = $this->request->param('offerareward', false);
        $where = [];

        // 开始时间
        if ($start) {
            $where['post.createtime'] = ['> time', $start];
        }

        // 结束时间  小于某个时间
        if ($end) {
            $where['post.createtime'] = ['<= time', $end];
        }
        // 时间区间
        if ($start && $end) {
            $where['post.createtime'] = ['between time', [$start, $end]];
        }
        // 关键字
        if ($keywords) {
            if (!$offerareward) {
                if ($keywords != '全部') {
                    $where['cate.name'] = ['like', "%$keywords%"];
                }
                $where['title'] = ['like', "%$keywords%"];
            } else {
                $where['title'] = ['like', "%$keywords%"];
            }
        }

        // 帖子分类列表
        $cate = $this->CateModel->select();

        // 帖子总数
        $count = $this->PostModel->onlyTrashed()->count();

        // query： 分页调用的时候传参，request请求传过来的参数
        $postlist = $this->PostModel->onlyTrashed()->with(['cate', 'user'])->where($where)->order('id', 'desc')->paginate(10, false, ['query' => Request::instance()->param()]);

        $pagestr = $postlist->render();

        $this->assign([
            'count' => $count,
            'postlist' => $postlist,
            'pagestr' => $pagestr,
            'start' => $start,
            'end' => $end,
            'offerareward' => $offerareward,
            'cate' => $cate,
            'keywords' => $keywords
        ]);

        return $this->fetch();
    }
    // 管理员
    public function aminrestore()
    {
        $start = $this->request->param('start', '');

        $end = $this->request->param('end', '');

        $keywords = $this->request->param('keywords', '');

        $where = [];

        // 开始时间
        if ($start) {
            $where['createtime'] = ['> time', $start];
        }

        // 结束时间  小于某个时间
        if ($end) {
            $where['createtime'] = ['<= time', $end];
        }
        // 时间区间
        if ($start && $end) {
            $where['createtime'] = ['between time', [$start, $end]];
        }
        // 关键字
        if ($keywords) {
            $where['username'] = ['like', "%$keywords%"];
        }

        $count = $this->AdminModel->onlyTrashed()->count();
        // query： 分页调用的时候传参，request请求传过来的参数
        $userlist = $this->AdminModel->onlyTrashed()->where($where)->order('id', 'desc')->paginate(10, false, ['query' => Request::instance()->param()]);

        $pagestr = $userlist->render();

        $this->assign([
            'count' => $count,
            'userlist' => $userlist,
            'pagestr' => $pagestr,
            'start' => $start,
            'end' => $end,
            'keywords' => $keywords
        ]);

        return $this->fetch();
    }
    // 商品
    public function product()
    {
        $keywords = $this->request->param('keywords', '');

        $where = [];

        // 关键字
        if ($keywords) {
            $where['name'] = ['like', "%$keywords%"];
        }

        $count = $this->ProductModel->onlyTrashed()->count();
        // query： 分页调用的时候传参，request请求传过来的参数
        $productlist = $this->ProductModel->onlyTrashed()->where($where)->order('id', 'desc')->paginate(10, false, ['query' => Request::instance()->param()]);
        foreach ($productlist as $item) {
            $item['flag'] = $item['flag'] == 1 ? '新品' : ($item['flag'] == 2 ? '热销' : ($item['flag'] == 3 ? '推荐' : ''));
        }
        $pagestr = $productlist->render();

        $this->assign([
            'count' => $count,
            'productlist' => $productlist,
            'pagestr' => $pagestr,
            'keywords' => $keywords
        ]);
        return $this->fetch();
    }
    // 用户相关还原
    public function reduction()
    {
        // 待还原数据总数
        $count = $this->UserModel->onlyTrashed()->count();

        // 是否请求成功
        if ($this->request->isPost()) {
            // id获取
            $id = $this->request->param('id', '');
            $id = explode(',', $id);
            // 消费充值状态
            $consumptionrecharge = $this->request->param('consumptionrecharge', false);
            // 管理员还原状态
            $admin = $this->request->param('aminrestore', false);

            $success = [
                'result' => false,
                'msg' => ''
            ];
            //还原数据
            $where = [
                'id' => ['in', $id]
            ];
            $restorecondition = $consumptionrecharge ? $this->RecordModel->onlyTrashed()->where($where)->update(['deletetime' => null]) : ($admin ? $this->AdminModel->onlyTrashed()->where($where)->update(['deletetime' => null]) : $this->UserModel->onlyTrashed()->where($where)->update(['deletetime' => null]));

            // 统计需还原数据
            if ($restorecondition) {
                $success['msg'] = '还原成功';
            } else {
                $success['msg'] = '还原失败';
            }
            $this->assign([
                'count' => $count
            ]);
            return $success;
        }
    }
    // 帖子评论还原
    public function postcommentrestore()
    {
        if ($this->request->isPost()) {

            $success = [
                'result' => false,
                'msg' => ''
            ];

            $id = $this->request->param('id', 0);
            $id = explode(',', $id);
            $postinfo = $this->PostModel->onlyTrashed()->find($id);
            if (!$postinfo) {
                $this->error('帖子不存在');
                exit;
            }
            $where = [
                'id' => ['in', $id]
            ];
            // 帖子表的还原
            $result = $this->PostModel->onlyTrashed()->where($where)->update(['deletetime' => null]);
            // 评论ID
            $ids = [];
            foreach ($id as $val) {
                // 查询该帖子下的所有评论
                $comment = $this->CommentModel->onlyTrashed()->where(['postid' => $val])->select();
                foreach ($comment as $item) {
                    $ids[] = $item['id'];
                }
            }
            $wheres = [
                'id' => ['in', $ids]
            ];
            // 评论表的还原
            $res = $this->CommentModel->onlyTrashed()->where($wheres)->update(['deletetime' => null]);

            if ($result === FALSE) {
                $success['result'] = false;
                $success['msg'] = '还原帖子失败';
            } else {
                $success['result'] = true;
                $success['msg'] = '还原帖子成功';
            }

            return $success;
        }
    }
    // 配置还原
    public function configrestore()
    {
        if ($this->request->isPost()) {
            $success = [
                'result' => false,
                'msg' => ''
            ];
            // 获取id
            $id = $this->request->param('id', 0);
            $id = explode(',', $id);
            $configinfo = $this->ConfigModel->onlyTrashed()->find($id);
            if (!$configinfo) {
                $this->error('配置不存在');
                exit;
            }
            $countlist = $this->ConfigModel->onlyTrashed()->where(['id' => ['in', $id]])->update(['deletetime' => null]);
            // 是否还原成功
            if ($countlist) {
                $success = [
                    'result' => true,
                    'msg' => '配置还原成功'
                ];
            } else {
                $success = [
                    'result' => false,
                    'msg' => '配置还原失败'
                ];
            }
            return $success;
        }
    }
    // 商品还原
    public function productreduction()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id', '');
            // 商品查询
            $product = $this->ProductModel->onlyTrashed()->find($id);
            if (!$product) {
                $this->error('商品不存在');
                exit;
            }
            $where = ['id' => ['in', explode(',', $id)]];
            // 商品还原
            $productdel = $this->ProductModel->onlyTrashed()->where($where)->update(['deletetime' => null]);
            if ($productdel) {
                $this->success('商品还原成功');
                exit;
            } else {
                $this->error('商品还原失败');
                exit;
            }
        }
    }
}
